package commandProcess;

import java.util.HashMap;
import java.util.Map;

import mapsObjects.FigureManager;

public class KeyCommandProcess implements I_CommandProcess {
	FigureManager _figureManager;
	Map<Integer, String> keyTable = new HashMap<Integer, String>();
	
	public KeyCommandProcess(FigureManager figureManager) {
		_figureManager = figureManager;
		initKeyTable();
	}
	
	private void initKeyTable()
	{
		keyTable.put(37, "left");
		keyTable.put(38, "up");
		keyTable.put(39, "right");
		keyTable.put(40, "down");
		keyTable.put(65, "left");
		keyTable.put(87, "up");
		keyTable.put(68, "right");
		keyTable.put(83, "down");

		keyTable.put(-32, "shoot");
		
		keyTable.put(-37, "released");
		keyTable.put(-38, "released");
		keyTable.put(-39, "released");
		keyTable.put(-40, "released");
		keyTable.put(-65, "released");
		keyTable.put(-87, "released");
		keyTable.put(-68, "released");
		keyTable.put(-83, "released");
	}
	
	@Override
	public void takeCommand(int commandCode) {
		if (keyTable.get(commandCode) == "shoot")
			_figureManager.shoot();
		else if (keyTable.get(commandCode) == "released")
			_figureManager.setIsButtonPressed(false);
		else if (!_figureManager.getIsButtonPressed() && _figureManager.getIsFittedToGrid() && keyTable.get(commandCode) != null)
		{
			_figureManager.setTankDirection(keyTable.get(commandCode));
			_figureManager.setIsButtonPressed(true);
		}
	}
}